unit clsVariacaoItemProducao;

interface

uses Dm, dbtables, biblio;

type
  TVariacaoItemProducao = class
  public
    Pro_Codigo2: string;
    Pro_Codigo3: string;
    Mostra_Desc: string;
    function Recuperar(Prod_Numero, Pro_Codigo, Sequencia, Pro_Codigo1: string): boolean;
    procedure Gravar(Prod_Numero, Pro_Codigo, Sequencia, Pro_Codigo1: string);
    procedure Excluir(Prod_Numero, Pro_Codigo, Sequencia: string);
    procedure Excluir_Todas(Prod_Numero: string);
    procedure Consultar(Prod_Numero, Pro_Codigo, Sequencia, Pro_Codigo1: string; var tq: TQuery);
    procedure Consultar1(Prod_Numero, Pro_Codigo, Sequencia: string; var tq: TQuery);
  end;

implementation

procedure TVariacaoItemProducao.Excluir;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := false;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from VariacaoItemProducao where Prod_Numero=:a and Pro_Codigo=:b and ' +
    'Sequencia=:c');
  tq.params[0].asstring := Prod_Numero;
  tq.params[1].asstring := Pro_Codigo;
  tq.params[2].asstring := Sequencia;
  tq.execsql;
  tq.close;
  tq.free;
end;

procedure TVariacaoItemProducao.Gravar;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from VariacaoItemProducao where Prod_Numero=:a and Pro_Codigo=:b and ' +
    'Sequencia=:c and Pro_Codigo1=:d');
  tq.params[0].AsString := Prod_Numero;
  tq.params[1].AsString := Pro_Codigo;
  tq.params[2].AsString := Sequencia;
  tq.params[3].AsString := Pro_Codigo1;
  tq.open;
  if tq.eof then begin
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Insert into VariacaoItemProducao(PROD_NUMERO,PRO_CODIGO,SEQUENCIA,PRO_CODIGO1, ' +
      'PRO_CODIGO2,PRO_CODIGO3,MOSTRA_DESC) ' +
      'values (:k_Prod_Numero,:k_Pro_Codigo,' +
      ':k_Sequencia,:k_Pro_Codigo1,:k_Pro_Codigo2,:k_Pro_Codigo3,:k_Mostra_Desc)');
  end else begin
    tq.close;
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('Update VariacaoItemProducao set Pro_Codigo2=:k_Pro_Codigo2,' +
      'Pro_Codigo3=:k_Pro_Codigo3,Mostra_Desc=:k_Mostra_Desc where ' +
      'Prod_Numero=:k_Prod_Numero and Pro_Codigo=:k_Pro_Codigo and ' +
      'Sequencia=:k_Sequencia and Pro_Codigo1=:k_Pro_Codigo1');
  end;
  tq.parambyname('k_Prod_Numero').asstring := Prod_Numero;
  tq.parambyname('k_Pro_Codigo').asstring := Pro_Codigo;
  tq.parambyname('K_Sequencia').asstring := Sequencia;
  tq.parambyname('k_Pro_Codigo1').asstring := Pro_Codigo1;
  tq.parambyname('k_Pro_Codigo2').asstring := Pro_Codigo2;
  tq.parambyname('k_Pro_Codigo3').asstring := Pro_Codigo3;
  tq.parambyname('k_Mostra_Desc').asstring := Mostra_Desc;
  tq.execsql;
  tq.close;
  tq.free;
end;

function TVariacaoItemProducao.Recuperar;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from VariacaoItemProducao where Prod_Numero=:a and Pro_Codigo=:b and ' +
    'Sequencia=:c and Pro_Codigo1=:d');
  tq.params[0].asstring := Prod_Numero;
  tq.params[1].asstring := Pro_Codigo;
  tq.params[2].asstring := Sequencia;
  tq.params[3].asstring := Pro_Codigo1;
  tq.open;
  if tq.eof then result := false else result := true;
  pro_codigo2 := tq.fieldbyname('pro_codigo2').asstring;
  pro_codigo3 := tq.fieldbyname('pro_codigo3').asstring;
  Mostra_Desc := tq.fieldbyname('Mostra_Desc').asstring;
  tq.close;
  tq.free;
end;

procedure TVariacaoItemProducao.Excluir_Todas;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := false;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from VariacaoItemProducao where Prod_Numero=:a');
  tq.params[0].asstring := Prod_Numero;
  tq.execsql;
  tq.close;
  tq.free;
end;

procedure TVariacaoItemProducao.Consultar;
begin
  tq.close;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select Pro_Codigo3,Mostra_Desc from VariacaoItemProducao where Prod_Numero=:a and ' +
    'Pro_Codigo=:b and Sequencia=:c and Pro_Codigo1=:d');
  tq.params[0].asstring := Prod_Numero;
  tq.params[1].asstring := Pro_Codigo;
  tq.params[2].asstring := Sequencia;
  tq.params[3].asstring := Pro_Codigo1;
end;

procedure TVariacaoItemProducao.Consultar1;
begin
  tq.close;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select Pro_Codigo3,Mostra_Desc from VariacaoItemProducao where Prod_Numero=:a and ' +
    'Pro_Codigo=:b and Sequencia=:c');
  tq.params[0].asstring := Prod_Numero;
  tq.params[1].asstring := Pro_Codigo;
  tq.params[2].asstring := Sequencia;
end;

end.
